﻿
CREATE PROCEDURE [megasys].[s_Tracker_Car_Insert_Cancel]
AS --SET NOCOUNT ON

BEGIN TRAN

DECLARE @TRs TABLE(TrackerID varchar(11))

INSERT INTO @TRs
SELECT     megasys.Martha.TrackerID
FROM         megasys.Martha INNER JOIN
                      t_Tracker ON megasys.Martha.TrackerID = t_Tracker.ID
WHERE     (megasys.Martha.Status = 'IN HOUSE') AND (NOT (megasys.Martha.TrackerID IS NULL)) AND (t_Tracker.StatusID  = 65)

DELETE FROM t_Tracker_Car WHERE TrackerID IN (SELECT TrackerID FROM @TRs)

UPDATE t_Tracker SET StatusID = 64 WHERE ID IN (SELECT TrackerID FROM @TRs)
UPDATE t_Tracker SET StatusID = 68 WHERE ID IN (SELECT TrackerID FROM @TRs)

COMMIT TRAN
